package com.itmate.jia;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class aulogin
 */
@WebServlet("/aulogin")
public class aulogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public aulogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Cookie[] cs=request.getCookies();
		String username="";
		String password="";
		for(int i=0;i<cs.length;i++){
			if("username".equals(cs[i].getName())){
				username=cs[i].getValue();
			}
			if("password".equals(cs[i].getName())){
				password=cs[i].getValue();
			}
		}
		if("admin".equals(username)&&"admin".equals(password)){
			HttpSession a = request.getSession();
			a.setAttribute("name", username);
			
			response.sendRedirect(request.getContextPath()+"/success.jsp");
		}else {
			response.sendRedirect(request.getContextPath()+"/fail.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
